//index.js
const app = getApp();
//引入数据API
const { index, goodsDetail } = require('../../utils/api.js');
 
Page({
  data: {
    //是否允许内容盒子竖向滚动
    scrollIndexContent: true,
    //是否允许大的scroll-view横向滚动
    scrollTap_x: false,
    //当前设备高度
    curr_height: '416px',
    //轮播图片
    swiperItem: '../../images/console-entrance.png',
    //template信息
    templateInfo: {
      name: 'topic',
      data: ''
    },
    //推荐页面数据
    recommand:{
      
    },
    //话题页面数据
    topic:{
      //话题数据
      topicList: [],
    },
    //品牌页面数据
    brand:{

    },
    //请求返回的所有数据
    indexData: {},
    //记录当前topBar子项下标
    topBar_index: 1,
    //topBar标签是否被激活
    topBar_active: []
  },

  //请求index数据
  req_indexData(){
    wx.request({
      url: index,
      success: (res) => {
        let indexData = res.data.data;
        this.setData({
          indexData: indexData,
          topic: {
            topicList: indexData.topicList
          },
          recommand: {
            //轮播图
            swiperItems: indexData.banner,
            //'频道'的数目
            channelItem_num: indexData.channel,
            //频道的图片
            channelImage: [
              'http://yanxuan.nosdn.127.net/92357337378cce650797444bc107b0f7.jpg',
              'http://yanxuan.nosdn.127.net/f4ff8b3d5b0767d4e578575c1fd6b921.jpg',
              'http://yanxuan.nosdn.127.net/cad5aba2bc52d3b8adfd0232c9814de2.jpg',
              'http://yanxuan.nosdn.127.net/003e1d1289f4f290506ac2aedbd09d35.jpg',
              'http://yanxuan.nosdn.127.net/99107fbd76eb66cd537213e478189ae1.png'
            ],
            //控制'频道'的scroll方向
            scrollChannel: false,
            //新品的数目
            new_prodCount: indexData.newGoodsList,
            //热门商品数
            hot_prodCount: indexData.hotGoodsList,
          },
          brand: {
            brandList: indexData.brandList
          }
        });
        //初始首屏渲染数据
        this.setData({
          topBar_active: [
            'active',
            '',
            ''
          ],
          //默认第一个
          topBar_index: 0
        });
      },
      fail: (err)=>{
        console.log(err);
        //重新发送请求
        // this.req_indexData();
      }
    })
  },

  onLoad: function() {
    //获取设备高度和宽度
    const systemInfo = wx.getSystemInfoSync();
    const curr_height = systemInfo.windowHeight - 40;
    const curr_width = systemInfo.windowWidth;
    const curr_dpr = systemInfo.pixelRatio; 
    console.log('height:',curr_height,'DPR:', curr_dpr);
    //初始化
    //设置scroll-view 内容容器的高度
    this.setData({
      curr_height: curr_height - 3 + 'px',
      curr_width,
      scrollWidth: curr_width*0
    });
    //设置topBar子项对应的模板名字和传入的数据
    this.setData({
      templateInfo: {
        name: 'recommand',
        data: this.data.recommand
      }
    });
    //获取index接口返回的数据
    this.req_indexData();
  },

  //切换顶部导航栏标签
  tapChange(event){
    let tagIndex = event.target.dataset.index;
    //点击同一个对象不变
    if (this.data.topBar_index == tagIndex){
      return;
    }
    this.setData({
      //开启X轴滚动开关
      scrollTap_x: true
    })

    //选择对象添加激活状态
    let topBar_active = ['','',''];
    topBar_active[tagIndex] = 'active';
    //更新数据
    this.setData({
      topBar_active: topBar_active,
      //更新当前选中的topBar子项下标
      topBar_index: tagIndex,
      //滚动到指定tap页
      scrollWidth: tagIndex * this.data.curr_width
    });

    this.setData({
      //关闭X轴滚动开关
      scrollTap_x: false
    })
  },
  //滑动开始
  touchStart(event) {
    // console.log(event);
    let clientX = event.touches[0].clientX;
    this.setData({
      clientX,
      //开启X轴滚动开关
      scrollTap_x: true
    })
  },
  //滑动中
  touchMove(){

  },
  //滑动结束
  touchEnd(event) {
    // console.log('touchEnd:', event.changedTouches[0].clientX, 'touchStart:', this.data.clientX, '相减', event.changedTouches[0].clientX - this.data.clientX);
    let currWidth = this.data.curr_width;
    let touchEnd_x = event.changedTouches[0].clientX;
    let touchStart_x = this.data.clientX;
    //向左滑
    let flagLeft = (touchEnd_x - touchStart_x) >= 40 && (touchEnd_x > touchStart_x);
    //向右滑
    let flagRight = (touchStart_x - touchEnd_x) >= 40 && (touchStart_x > touchEnd_x);
    //获取当前tap页下标, 激活状态重置
    let topBar_index, topBar_active;

    // console.log('flagLeft:', flagLeft, 'flagRight:', flagRight)
    //获取当前tap页下标
    topBar_index = this.data.topBar_index;

    if (flagLeft) {
      // console.log('从左边开始滑动')
      //第一个tap从左边开始滑动无效
      if (this.data.topBar_index == 0) {
        // console.log('不能再向左滑动了')
        return;
      }
      //更改当前tap页的下标
      topBar_index = topBar_index - 1;
      //更改激活状态
      topBar_active = ['', '', '']
      topBar_active[topBar_index] = 'active';

    }

    if (flagRight) {
      // console.log('从右边开始滑动')
      //最后一个tap从右边开始滑动无效
      if (this.data.topBar_index == 2) {
        // console.log('不能再向右滑动了')
        return;
      }

      topBar_index = topBar_index + 1;
      topBar_active = ['', '', '']
      topBar_active[topBar_index] = 'active';
    }
    //重置位置到当前页面 1.左滑或者右滑距离太短 2.点击而非滑动
    if ((flagRight && flagLeft) || ( touchEnd_x == touchStart_x) ){
      console.log('重置页面')
      topBar_active = this.data.topBar_active;
      this.setData({
        scrollWidth: currWidth * this.data.topBar_index
      })
    }

    this.setData({
      topBar_index: topBar_index,
      topBar_active: topBar_active,
      scrollWidth: currWidth * topBar_index,
      //关闭X轴滚动开关
      scrollTap_x: false
    })
  },

  //阅读全文功能
  readAll(event){
    let id = event.currentTarget.dataset.id;
    app.globalData.indexData = this.data.indexData;
    wx.navigateTo({
      url: `../topic/topic?id=${id}`,
    })
  },
  //调到对应的频道页面
  to_channel(event){
    let url = event.currentTarget.dataset.url;
    console.log(url);
    let id = url.match(/=(\d+)/)["1"];
    console.log(id)
  },
  //新品跳转至详情
  new_prodetail(event){
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../prodetail/prodetail?id=${id}`
    })
  },
  //更多新品
  new_more(){
    app.globalData.type = "new";
    wx.switchTab({
      url: '../catalog/catalog',
    })
  },
  //热门商品跳转至详情
  hot_prodetail(event){
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../prodetail/prodetail?id=${id}`
    })
  },
  //更多热门商品
  hot_more(){
    app.globalData.type = "hot";
    wx.switchTab({
      url: '../catalog/catalog',
    })
  },

})
